home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
util
/
urllib2_file.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
6KB
|
234 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from urllib2 import URLError
from urllib import splittype
from urllib import splithost
from httplib import NotConnected
from urllib import addinfourl
import os
import socket
import sys
import stat
import mimetypes
import mimetools
import httplib
import urllib
import urllib2
CHUNK_SIZE = 65536
def get_content_type(filename):
if not mimetypes.guess_type(filename)[0]:
pass
return 'application/octet-stream'
def send_data(v_vars, v_files, boundary, sendfunc = None):
l = 0
for k, v in v_vars:
buffer = ''
buffer += '--%s\r\n' % boundary
buffer += 'Content-Disposition: form-data; name="%s"\r\n' % k
buffer += '\r\n'
buffer += v + '\r\n'
if sendfunc:
sendfunc(buffer)
l += len(buffer)
for k, v in v_files:
fd = v
if hasattr(fd, 'len'):
file_size = fd.len
else:
file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
if isinstance(k, tuple):
(k, name) = k
else:
name = fd.name.split('/')[-1]
if isinstance(name, unicode):
name = name.encode('UTF-8')
buffer = ''
buffer += '--%s\r\n' % boundary
buffer += 'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (k, name)
buffer += 'Content-Type: %s\r\n' % get_content_type(name)
buffer += 'Content-Length: %s\r\n' % file_size
buffer += '\r\n'
l += len(buffer)
if sendfunc:
sendfunc(buffer)
if hasattr(fd, 'seek'):
fd.seek(0)
while True:
chunk = fd.read(CHUNK_SIZE)
if not chunk:
break
sendfunc(chunk)
l += file_size
buffer = '\r\n'
buffer += '--%s--\r\n' % boundary
buffer += '\r\n'
if sendfunc:
sendfunc(buffer)
l += len(buffer)
return l
def do_request_(self, request):
host = request.get_host()
if not host:
raise URLError('no host given')
data = request.get_data()
v_files = []
v_vars = []
if request.has_data() and not isinstance(data, str):
if hasattr(data, 'items'):
data = data.items()
else:
try:
if len(data) and not isinstance(data[0], tuple):
raise TypeError
except TypeError:
(_ty, _va, tb) = sys.exc_info()
raise TypeError, 'not a valid non-string sequence or mapping object: %r' % type(data), tb
for k, v in data:
if hasattr(v, 'read'):
v_files.append((k, v))
continue
v_vars.append((k, v))
boundary = mimetools.choose_boundary()
request.boundary = boundary
request.v_files = v_files
request.v_vars = v_vars
if len(v_vars) > 0 and len(v_files) == 0:
request.data = data = urllib.urlencode(v_vars)
v_files[:] = []
v_vars[:] = []
if request.has_data():
if 'Content-type' not in request.headers:
if len(v_files) > 0:
l = send_data(v_vars, v_files, boundary)
request.add_unredirected_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
request.add_unredirected_header('Content-length', str(l))
else:
request.add_unredirected_header('Content-type', 'application/x-www-form-urlencoded')
if 'Content-length' not in request.headers:
request.add_unredirected_header('Content-length', '%d' % len(data))
(_scheme, sel) = splittype(request.get_selector())
(sel_host, _sel_path) = splithost(sel)
if not request.has_header('Host'):
if not sel_host:
pass
request.add_unredirected_header('Host', host)
for name, value in self.parent.addheaders:
name = name.capitalize()
if not request.has_header(name):
request.add_unredirected_header(name, value)
continue
return request
urllib2.AbstractHTTPHandler.do_request_ = do_request_
old_open = urllib2.AbstractHTTPHandler.do_open
def do_open(self, http_class, req):
req = do_request_(self, req)
host = req.get_host()
if not host:
raise URLError('no host given')
h = http_class(host)
h.set_debuglevel(self._debuglevel)
headers = dict(req.headers)
headers.update(req.unredirected_hdrs)
headers['Connection'] = 'close'
headers = dict((lambda .0: for name, val in .0:
(name.title(), val))(headers.items()))
if req.has_data() and not isinstance(req.data, str):
reqdata = req
else:
reqdata = req.data
try:
h.request(req.get_method(), req.get_selector(), reqdata, headers)
r = h.getresponse()
except socket.error:
err = None
raise URLError(err)
r.recv = r.read
fp = socket._fileobject(r, close = True)
resp = addinfourl(fp, r.msg, req.get_full_url())
resp.code = r.status
resp.msg = r.reason
return resp
urllib2.AbstractHTTPHandler.do_open = do_open
def _send_request(self, method, url, body, headers):
header_names = []([ k.lower() for k in headers ])
skips = { }
if 'accept-encoding' in header_names:
skips['skip_accept_encoding'] = 1
self.putrequest(method, url, **skips)
if isinstance(body, str):
if body and 'content-length' not in header_names:
self.putheader('Content-Length', str(len(body)))
for hdr, value in headers.iteritems():
self.putheader(hdr, value)
self.endheaders()
if body:
self.send(body)
httplib.HTTPConnection._send_request = _send_request
def send(self, str):
if self.sock is None:
if self.auto_open:
self.connect()
else:
raise NotConnected()
if self.debuglevel > 0:
print 'send:', repr(str)
try:
if hasattr(str, 'boundary'):
boundary = str.boundary
v_files = str.v_files
v_vars = str.v_vars
send_data(v_vars, v_files, boundary, self.sock.sendall)
else:
self.sock.sendall(str)
except socket.error:
v = None
if v[0] == 32:
self.close()
raise
httplib.HTTPConnection.send = send